package com.example.administrator.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.myapplication.uilts.PeiZhiActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 董豪华 on 2017/1/6.
 */
public class YearAcitivity extends AppCompatActivity {
    private TextView nameyear;
    private TextView dateyear;
    private TextView infoyear;
    private TextView textyear;
    private TextView careeryear;
    private TextView loveyunyear;
    private TextView financeyunyear;
    String xingzuo;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yearxingzuo);
        Intent intent = getIntent();
        xingzuo = intent.getStringExtra("xingzuo");
        jiazai();
        shujufanhui(xingzuo);
    }

    private void jiazai() {
        nameyear = (TextView) findViewById(R.id.nameyear);
        dateyear = (TextView) findViewById(R.id.dateyear);
        infoyear = (TextView) findViewById(R.id.info);
        textyear = (TextView) findViewById(R.id.text);
        careeryear = (TextView) findViewById(R.id.career);
        loveyunyear = (TextView) findViewById(R.id.loveyunyear);
        financeyunyear = (TextView) findViewById(R.id.financeyunyear);
    }
    public void shujufanhui(String consName){
        String url = "http://web.juhe.cn:8080/constellation/getAll?consName="+consName+"&type=year&key=4ab3d0b2232c72d9c3dc4747baf8cf4f";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                jiexiyear(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(YearAcitivity.this,"数据异常",Toast.LENGTH_LONG).show();
            }
        });
        PeiZhiActivity.mrequestQueue.add(request);
    }
    /*年*/
    public void jiexiyear(String response){
        try {
            JSONObject jsonObject2 = new JSONObject(response);
            String name = jsonObject2.optString("name");
            String date = jsonObject2.optString("date");
            String year = jsonObject2.optString("year");
            String resultcode = jsonObject2.optString("resultcode");
            String error_code = jsonObject2.optString("error_code");
            JSONObject mima = jsonObject2.optJSONObject("mima");
            String info = mima.optString("info");
            JSONArray text = mima.optJSONArray("text");
            JSONArray career = jsonObject2.optJSONArray("career");
            JSONArray love = jsonObject2.optJSONArray("love");
            JSONArray finance = jsonObject2.optJSONArray("finance");
            String s = "";
            String s1 = "";
            String s2 = "";
            String s3 = "";
            for (int i = 0 ;i <text.length();i++){
                s = text.optString(i);
            }
            for (int i = 0 ;i <career.length();i++){
                s1 = career.optString(i);
            }
            for (int i = 0 ;i <love.length();i++){
                s2 = love.optString(i);
            }
            for (int i = 0 ;i <finance.length();i++){
                s3 = finance.optString(i);
            }
            nameyear.setText(name);
            dateyear.setText(date);
            infoyear.setText(info);
            textyear.setText(s);
            careeryear.setText("事业运:\n"+s1);
            loveyunyear.setText("感情运:\n"+s2);
            financeyunyear.setText("财运:\n"+s3);
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(YearAcitivity.this,"解析异常",Toast.LENGTH_LONG).show();
        }
    }
}
